package edu.nju.desserthouse.servlets;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.nju.desserthouse.action.business.SaleInfoBean;
import edu.nju.desserthouse.dao.SaleDao;
import edu.nju.desserthouse.dao.ShopDao;

/**
 * Servlet implementation class SaleServlet
 */
@WebServlet("/salestatistics.manager")
public class SaleServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
	
	@EJB
	SaleDao saleDao;
	@EJB
	ShopDao shopDao;
	
    public SaleServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		execute (request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		execute (request, response);
	}
	
	public void execute(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
		SaleInfoBean saleInfo = new SaleInfoBean();
		long shopNum = shopDao.getShopNum() + 1;
		saleInfo.setShopNum(shopNum);
		saleInfo.initSaleInfoList(shopNum);
		saleInfo.setSaleList(saleDao.getSaleList());
		for(int i = 0; i < saleInfo.getSaleSize(); i++){
			for(int j = 0; j < saleInfo.getListSize(); j++){
				if(saleInfo.getSaleShop(i) == j){
					saleInfo.addToSaleNum(j); 
					saleInfo.addToSaleIncome(j, saleInfo.getSalePrice(i));
				}
			}
		}
		
		double totalSale = 0;
		double totalIncome = 0;
		
		for(int i = 0; i < saleInfo.getListSize(); i++){
			totalSale += saleInfo.getSaleNum(i);
			totalIncome += saleInfo.getSaleIncome(i);
		}
		
		saleInfo.setTotalSale(Integer.parseInt(new java.text.DecimalFormat("0").format(totalSale)));
		
		if(totalSale != 0){
			for(int i = 0; i < saleInfo.getListSize(); i++){
				double ratio = saleInfo.getSaleNum(i)/totalSale * 100;
				saleInfo.setNumRatio(i, ratio);
			}
		}else{
			System.out.println("No Sale!!!");
		}
		
		saleInfo.setTotalIncome(totalIncome);
		
		if(totalIncome != 0){
			for(int i = 0; i < saleInfo.getListSize(); i++){
				double ratio = saleInfo.getSaleIncome(i)/totalIncome * 100;
				saleInfo.setIncomeRatio(i, ratio);
			}
		}else{
			System.out.println("No income!");
		}
		
		req.getSession().setAttribute("saleInfo", saleInfo);
		
		resp.sendRedirect("/desserthouseWEB/manager/salestatistics.jsp");
	}
}
